package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.IMemberService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 *  用户登录controller
 *
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Reference(timeout = 5000,retries = 0,check = false)
    ILoginService loginService;

    @Reference(timeout = 5000,retries = 0,check = false)
    IMemberService memberService;

    @Reference(timeout = 3000)
    IKaptchaService kaptchaService;

    /**
     *  用户登录
     *
     * @return
     */
    @PostMapping("login")
    @Anoymous
    public ResponseData userLogin(@RequestBody Map<String,String> map,
                                  HttpServletRequest request, HttpServletResponse response){
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String kaptcha = map.get("captcha");

        // 1. 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request,"kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(kaptcha);
        KaptchaCodeResponse kaptchaResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = kaptchaResponse.getCode();
        if(!code.equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 验证码校验错误
            return  new ResponseUtil<>().setErrorMsg(kaptchaResponse.getMsg());
        }
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);
        // 2. 验证⽤户名以及密码
        // 3. 产⽣⼀个合法的JWT
        UserLoginResponse loginResponse = loginService.userLogin(userLoginRequest);
        // 根据code值判断是否成功
        if(!loginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setErrorMsg(loginResponse.getMsg());
        }
        // 将token存储到cookie中
        String jwtTokenStr = loginResponse.getToken();
        Cookie cookie = CookieUtil.genCookie("access_token", jwtTokenStr, "/", 3600);
        cookie.setHttpOnly(true);
        CookieUtil.setCookie(response,cookie);

        return new ResponseUtil<>().setData(loginResponse,"成功");
    }

    /**
     *  验证用户登录
     *
     * @return
     */
    @GetMapping("login")
    @Anoymous
    public ResponseData loginVerify(HttpServletRequest request){
        CheckAuthRequest checkAuthRequest = new CheckAuthRequest();
        String token = CookieUtil.getCookieValue(request,"access_token");
        if(token == null){
            return new ResponseUtil<>().setErrorMsg("token为空，用户未登录");
        }
        checkAuthRequest.setToken(token);
        CheckAuthResponse checkAuthResponse = loginService.validToken(checkAuthRequest);
        UserVerifyResponse verifyResponse = memberService.getUserVerifyInfo(Integer.valueOf((String)checkAuthResponse.getUserInfo().get("uid")));
        if(!verifyResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 发生错误
            return new ResponseUtil<>().setErrorMsg(verifyResponse.getMsg());
        }
        return new ResponseUtil<>().setData(verifyResponse);
    }

}
